<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="831px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" :rules="formRules">
                <el-form-item label-width="160px" label="发明人姓名" prop="chineseName">
                    <el-input
                        class="w-[760px]"
                        v-model="formData.chineseName"
                        placeholder="可填写多个发明人姓名，用；或，或、分割，不指定分割符，默认所有。"
                    />
                </el-form-item>
                <el-form-item label-width="160px" label="指定分隔符" prop="entityType">
                    <el-select
                        class="flex-1 w-[760px]"
                        v-model="separator"
                        placeholder="请选择指定分隔符"
                    >
                        <el-option label=";(英文分号)" value="1" />
                        <el-option label="；(中文分号)" value="2" />
                        <el-option label="，（中文逗号）" value="3" />
                        <el-option label=",（英文逗号）" value="4" />
                    </el-select>
                </el-form-item>
                <el-form-item label-width="160px" label="发明人国籍(地区)" prop="nationality">
                    <el-input class="w-[760px]" v-model="formData.nationality" placeholder="请输入国籍(地区)" />
                </el-form-item>

                <el-form-item label-width="160px" label="第一发明人证件号码" prop="idNumber">
                    <el-input class="w-[760px]" v-model="formData.idNumber" placeholder="请输入证件号码" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { inventorEdit, inventorAdd, inventorDetail } from '@/api/inventor'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const separator = ref('')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '修改发明人' : '添加发明人'
})

const formData = reactive({
    id: '',
    customerNameId: '',
    customerName: '',
    chineseName: '',
    englishName: '',
    jobNumber: '',
    surnameChinese: '',
    nameChinese: '',
    surnameEnglish: '',
    nameEnglish: '',
    nationality: '',
    isTechnicist: '',
    nation: '',
    province: '',
    cityChinese: '',
    cityEnglish: '',
    streetChinese: '',
    streetEnglish: '',
    departmentChinese: '',
    departmentEnglish: '',
    email: '',
    phone: '',
    certificateType: '',
    idNumber: '',
    postcode: '',
    landlinePhone: '',
    qq: '',
    creatorId: '',
    creatorName: '',
    creatorTime: '',
    updateStaffId: '',
    updateStaff: '',
    contentUpdateTime: '',
    remark: ''
})

const formRules = {
    chineseName: [
        {
            required: true,
            message: '请输入中文名',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await inventorEdit(data) : await inventorAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await inventorDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
